package cn.kgc.spring.convertors;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 课工场
 * @date 2024/4/28
 * @description   自定义日期类型转换器    String ->  Date   Converter
 */
public class DateConverter implements Converter<String, Date> {

    private String pattern;

    public void  setPattern(String pattern){
        this.pattern = pattern;
    }

    @Override
    public Date convert(String source) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            Date parse = simpleDateFormat.parse(source);
            return parse;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
